package top.yuechenc.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import top.yuechenc.manage.entity.SpManager;
import top.yuechenc.manage.entity.SpManagerDto;
import top.yuechenc.manage.mapper.SpManagerMapper;
import top.yuechenc.manage.service.ManagerService;

import java.util.List;

/**
 * @author wangzhiwei
 */
@Service
public class ManagerServiceImpl implements ManagerService {
    @Autowired
    private SpManagerMapper managerMapper;

    @Override
    public SpManagerDto getManagerByUsername(String mgName) {
        return managerMapper.selectByUsername(mgName);
    }

    @Override
    public List<SpManager> findAll() {
        return managerMapper.selectAll();
    }

    @Override
    public SpManager findManagerById(String mgId) {
        return managerMapper.selectByPrimaryKey(mgId);
    }

    @Override
    public PageInfo findListByParam(SpManager spManager, int pageNum,int pageSize) {
        Example example=new Example(SpManager.class);
        Example.Criteria criteria=example.createCriteria();
        if(spManager.getMgName()!=null && !"".equals(spManager.getMgName())){
            criteria.andLike("mgName","%"+spManager.getMgName()+"%");
        }

        if(spManager.getMgState()!=null &&!"".equals(spManager.getMgState())){
            criteria.andEqualTo("mgState",spManager.getMgState());
        }
        example.orderBy("mgTime");

        PageHelper.startPage(pageNum, pageSize);
        List<SpManager> spManagerList = managerMapper.selectByExample(example);
        return new PageInfo<SpManager>(spManagerList);
    }

    @Override
    public void addManager(SpManager spManager) {
        managerMapper.insertSelective(spManager);
    }

    @Override
    public void updateState(SpManager manager) {
        managerMapper.updateByPrimaryKeySelective(manager);
    }

    @Override
    public void updById(SpManager manager) {
        managerMapper.updateByPrimaryKeySelective(manager);
    }

    @Override
    public void delById(String mgId) {
        managerMapper.deleteByPrimaryKey(mgId);
    }
}
